<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Carbon\Carbon;
use Think\Model\RelationModel;

class ActivityModel extends RelationModel {

    /**
     * 分页查询
     * @param $param
     * @return array
     */
    function getData($param){
        // 请求参数
        if($param['key']){
            $where['a.name'] = ['like', '%'.$param['key'].'%'];
            $where['b.company_ak'] = ['like', '%'.$param['key'].'%'];
            $where['b.name'] = ['like', '%'.$param['key'].'%'];
            $where['b.mobile'] = ['like', '%'.$param['key'].'%'];
        }

        $where2 = [];
        if($param['audit_user_id']){
            $where2['audit_user_id'] = $param['audit_user_id'];
        }
        if($where && $where2){
            $where['_logic'] = 'or';
            $where2['_complex'] = $where;
        }elseif($where && !$where2){
            $where['_logic'] = 'or';
            $where2 = $where;
        }elseif(!$where && !$where2){
            $where2 = [];
        }

        // 获取数据
        $pageNum = ($param['page'] - 1) * $param['size'];
        $data['count'] = $this->alias('a')
            ->join('LEFT JOIN __AUDIT__ b ON a.audit_user_id = b.id')
            ->where($where2)->count();
        if($data['count'] < $pageNum){
            return ['status' => false, 'msg'=>'没有更多数据了'];
        }
        $data['data'] = $this->alias('a')
            ->field('a.*, b.name audit_name, b.mobile audit_mobile, b.company_ak')
            ->join('LEFT JOIN __AUDIT__ b ON a.audit_user_id = b.id')
            ->where($where2)
            ->order('a.add_time desc')
            ->limit($pageNum, $param['size'])
            ->select();

        foreach ($data['data'] as &$v){
            $v['all_store_count'] = count(explode(',', $v['store_ids']));
            $v['store_count'] = M('order')->where(['activity_id'=>$v['id'], 'order_status'=>['gt', 0]])->count();
            $v['store_count_info'] = $v['store_count'].'/'.$v['all_store_count'];
        }
        unset($v);
        return ['status'=> true, 'data'=> $data];
    }

    /**
     * 获取详情
     * @param $id
     * @return bool
     */
    public function getInfoById($id){
        $data = $this->alias('a')
            ->field('a.*, 
            b.name audit_name, b.mobile audit_mobile')
            ->join('LEFT JOIN __AUDIT__ b ON a.audit_user_id = b.id')
            ->where(['a.id'=>$id])->find();
        $data['all_store_count'] = count(explode(',', $data['store_ids']));
        $data['store_count'] = M('order')->where(['activity_id'=>$data['id'], 'order_status'=>['gt', 0]])->count();
        return $data ? : false;
    }

    /**
     * 删除活动
     * @return bool
     */
    public function deleteById($id){
        M()->startTrans();
        $res1 = M('order')->where(['activity_id'=>$id])->delete();
        $res2 = $this->where(['id'=>$id])->delete();
        if($res1!==false && $res2){
            M()->commit();
            return true;
        }else{
            M()->rollback();
            return false;
        }
    }
}
